package activity.personal.qianbao;

import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.ProgressBar;

import com.itboye.hutoubenjg.R;

import java.util.Observable;
import java.util.Observer;

import util.BaseOtherActivity;
import util.utls.AutoResizeTextView;

import static util.utls.Const.webRegisterUrl;


public class WebActivity extends BaseOtherActivity implements Observer,
        OnClickListener {

    WebView webview;
    AutoResizeTextView tvTitle;
    private String urls=webRegisterUrl;
    private String web_title;
    ImageView back;
    ProgressBar myProgressBar;
    WebSettings settings;
    StringBuilder builder = new StringBuilder();


    @Override
    protected int layout_id() {
        return R.layout.activity_web;
    }

    @Override
    protected void onInitController() {

    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) {
            webview.goBack(); // goBack()表示返回WebView的上一页面
            return true;
        } else {
            finish();
        }

        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
            case R.id.back:
                finish();
                break;

            default:
                break;
        }
    }

    @Override
    public void update(Observable observable, Object data) {
        // TODO Auto-generated method stub

    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
    }

    @Override
    protected void onInit() {
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE |
                WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

        web_title = getIntent().getStringExtra("title");
        tvTitle.setText("注册");
        settings = webview.getSettings();
        settings.setSupportZoom(true);
        settings.setUseWideViewPort(true);
        settings.setJavaScriptEnabled(true);//支持js

        settings.setLoadWithOverviewMode(true);
        if (urls == null) {
            urls = "http://www.baidu.com";
        }

        webview.setWebViewClient(new WebViewClient() {

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                // TODO Auto-generated method stub
                builder = new StringBuilder();
                builder.append(url);
                Log.e("hu", "url==" + url);
                if (url.contains("BACK")) {
                    finish();
                } else {
                    view.loadUrl(url);
                }

                return true;
            }
        });

        webview.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (newProgress == 100) {
                    myProgressBar.setVisibility(View.GONE);
                } else {
                    if (View.GONE == myProgressBar.getVisibility()) {
                        myProgressBar.setVisibility(View.VISIBLE);
                    }
                    myProgressBar.setProgress(newProgress);
                }
                super.onProgressChanged(view, newProgress);
            }

        });
        webview.loadUrl(urls);
    }

    @Override
    protected void onReadIntent(Bundle bundle) {

    }
}
